package com.atguigu.acyjl.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;

import com.atguigu.acyjl.EasyDao;
import com.atguigu.acyjl.EasyIdiom;
import com.atguigu.acyjl.Idiom;
import com.atguigu.acyjl.Lose;
import com.atguigu.acyjl.OpeningAnimation;
import com.atguigu.acyjl.PlaySounds;
import com.atguigu.acyjl.R;
import com.atguigu.acyjl.Settings;
import com.atguigu.acyjl.Win;
import com.atguigu.acyjl.WordDao;
import com.atguigu.acyjl.WordDatabase;
import com.atguigu.acyjl.constants.Constant;
import com.atguigu.acyjl.policy.Policy;
import com.atguigu.acyjl.policy.SPUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    public WordDatabase wordDatabase;
    public WordDao wordDao;
    public EasyDao easyDao;

    int countLose = 0;
    public static MainActivity mainActivity;//传递给非activity的类使用  救命
    public static Context mContext;//传递给非activity的类使用
    //以下两行要写在onCreate
    //mainActivity=this;//传递给非activity的类使用
    //mContext=this.getBaseContext();//传递给非activity的类使用


    public OpeningAnimation openingAnimation = new OpeningAnimation();
    public Settings settings = new Settings();
    Win win = new Win();



    public Button btn11, btn12, btn13, btn14, btn15, btn16, btn21, btn22, btn23, btn24, btn25, btn26, btn31, btn32, btn33,
            btn34, btn35, btn36, btn41, btn42, btn43, btn44, btn45, btn46, btn51, btn52, btn53, btn54, btn55, btn56,
            btn61, btn62, btn63, btn64, btn65, btn66;
    public Button btnStars;
    public Button word1, word2, word3, word4, word5, word6, word7, word8, locator;

    public PlaySounds playSounds = new PlaySounds();



    public static SharedPreferences shp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        mainActivity = this;//传递给非activity的类使用
        mContext = this.getBaseContext();//传递给非activity的类使用

        wordDatabase = Room.databaseBuilder(this, WordDatabase.class, "Idiom_database")
                .allowMainThreadQueries().fallbackToDestructiveMigration().build();
        wordDao = wordDatabase.getWordDao();
        easyDao = wordDatabase.getEasyDao();


        List<Idiom> list = wordDao.getAllWords();//为了产生database
        List<EasyIdiom> easyList = easyDao.getAllWords();//为了产生database
        settings.getList(list);//为了产生database
        settings.getEasyList(easyList);//为了产生database


        //db导入工程内
        String dbPath = getApplicationContext().getDatabasePath("Idiom_database.db").getPath();
        File dbFile = getDatabasePath("Idiom_database.db");
        if (!dbFile.exists()) {
            try {
                InputStream inputStream = getAssets().open("database_name.db");
                OutputStream outputStream = new FileOutputStream(dbFile);

                byte[] buffer = new byte[1024];
                int length;
                while ((length = inputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, length);
                }

                outputStream.flush();
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        btn11 = findViewById(R.id.btn11);
        btn12 = findViewById(R.id.btn12);
        btn13 = findViewById(R.id.btn13);
        btn14 = findViewById(R.id.btn14);
        btn15 = findViewById(R.id.btn15);
        btn16 = findViewById(R.id.btn16);
        btn21 = findViewById(R.id.btn21);
        btn22 = findViewById(R.id.btn22);
        btn23 = findViewById(R.id.btn23);
        btn24 = findViewById(R.id.btn24);
        btn25 = findViewById(R.id.btn25);
        btn26 = findViewById(R.id.btn26);
        btn31 = findViewById(R.id.btn31);
        btn32 = findViewById(R.id.btn32);
        btn33 = findViewById(R.id.btn33);
        btn34 = findViewById(R.id.btn34);
        btn35 = findViewById(R.id.btn35);
        btn36 = findViewById(R.id.btn36);
        btn41 = findViewById(R.id.btn41);
        btn42 = findViewById(R.id.btn42);
        btn43 = findViewById(R.id.btn43);
        btn44 = findViewById(R.id.btn44);
        btn45 = findViewById(R.id.btn45);
        btn46 = findViewById(R.id.btn46);
        btn51 = findViewById(R.id.btn51);
        btn52 = findViewById(R.id.btn52);
        btn53 = findViewById(R.id.btn53);
        btn54 = findViewById(R.id.btn54);
        btn55 = findViewById(R.id.btn55);
        btn56 = findViewById(R.id.btn56);
        btn61 = findViewById(R.id.btn61);
        btn62 = findViewById(R.id.btn62);
        btn63 = findViewById(R.id.btn63);
        btn64 = findViewById(R.id.btn64);
        btn65 = findViewById(R.id.btn65);
        btn66 = findViewById(R.id.btn66);
        btnStars = findViewById(R.id.btnStars);
        word1 = findViewById(R.id.word1);
        word2 = findViewById(R.id.word2);
        word3 = findViewById(R.id.word3);
        word4 = findViewById(R.id.word4);
        word5 = findViewById(R.id.word5);
        word6 = findViewById(R.id.word6);
        word7 = findViewById(R.id.word7);
        word8 = findViewById(R.id.word8);
        locator = findViewById(R.id.locator);


        playSounds.initSound(this);
        shp = getSharedPreferences(Constant.KEY_SHAREDPREFERENCE_NAME,MODE_PRIVATE);
        settings.score = shp.getString(Constant.KEY_MAIN_SCORE,"0456");


        final Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                // 在此处编写需要实时调用的方法

                if(settings.count>=6 && !settings.winFlag && countLose==0){
                    isLose();
                    countLose++;
                }
                handler.postDelayed(this, 1000); // 每隔1秒执行一次
            }
        };

// 在需要开始定时任务的地方调用以下代码
        handler.postDelayed(runnable, 1000); // 延迟1秒后开始执行



    }//onCreate





    int count=0;
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {//
        super.onWindowFocusChanged(hasFocus);

        //恢复按钮样式
        if(count==0){
            settings.StartGame();//完成开始前的各种设置
            count++;
        }
        settings.Click();

    }



    public void isWin(){

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(MainActivity.this,Win.class);
                startActivity(i);
            }
        },500);
    }
    public void isLose(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(MainActivity.this, Lose.class);
                startActivity(i);
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        //必须先开一次market
        SharedPreferences shp = getSharedPreferences("oneData",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor =shp.edit();
        editor.putString(Constant.KEY_MAIN_SCORE,settings.score);
        editor.apply();
    }
}